﻿CREATE PROCEDURE [dbo].[proc_Project_Getlist_fyyxsr_xx]
	(
		@CompanyId int,
		@StartDate nvarchar(10),
		@EndDate nvarchar(10),
		@Audit int,
		@Stext nvarchar(50),
		@StartIndex int,
		@EndIndex INT,
		@TypeId INT,
		
		@OId uniqueidentifier
	)
AS
Begin
	;WITH list as (
		
		SELECT ROW_NUMBER() OVER (ORDER BY FinanceAudit,IsFinish DESC)AS Row	,* FROM (
		Select id,ProjectId
		,OrderId
		,CusName
		,ProductName
		,SupplierName
		,CostPrice
		,TypeName
		,IsFinish
		,SalePrice
		,ISNULL(FinishDate,'1900-01-01') As FinishDate
		,FinanceAudit
		,ISNULL(FinanceAuditDate,'1900-01-01') As FinanceAuditDate
		,Salesman
		,CreateDate
		,(SalePrice-CostPrice) AS a
		From Project
 WHERE ProductId IN (22,29,33)  and ( CompanyId=@CompanyId) 
 and Convert(nchar(10),CreateDate,120) between @StartDate and @EndDate
		and SalePrice<>CostPrice AND OrderId=@OId
	
	
		) AS ab
	)
		
	Select *,(Select Count(1) From list) AS DataCount
		,(Select Sum(a) From list) AS CostPrice_SUM 
--(Select Sum(SalePrice-CostPrice) From Project WHERE TypeId= @TypeId  and ( CompanyId=@CompanyId) and Convert(nchar(10),CreateDate,120) between @StartDate and @EndDate) AS CostPrice_SUM 	
	From list Where Row between @StartIndex and @EndIndex  
End
	RETURN
